package com.ogawa.project6602.adapter

import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.ogawa.base.utils.AppUtil
import com.ogawa.project6602.R
import com.ogawa.projectcommon.bean.CollectBean
import com.ogawa.projectcommon.dboperation.OperationDB

class CollectAdapter : BaseQuickAdapter<CollectBean, BaseViewHolder>(R.layout.layout_collect) {
    private var currentProgram: CollectBean = CollectBean()

    override fun getItemCount(): Int {
        return if (data.size > 3) {
            3
        } else {
            data.size
        }
    }

    override fun convert(helper: BaseViewHolder, item: CollectBean?) {
        helper.setText(R.id.tv_collect, item?.programName)
        val program =
            OperationDB().db.getProgram8506EDao()
                .getProgramByTypeAndModel(item!!.program, item.type)
        if (program.imageName != null) {
            val resourceId =
                AppUtil.getResourceIdByName(
                    mContext.resources,
                    program.imageName,
                    "drawable",
                    mContext.packageName
                )
            if (resourceId != 0) {
                AppUtil.setTextDrawableTop(
                    mContext.resources,
                    helper.getView(R.id.tv_collect),
                    resourceId
                )
            }
        }
        helper.getView<TextView>(R.id.tv_collect).isSelected =
            true && currentProgram.program == item.program && currentProgram.type == item.type
    }

    fun setCurrentProgram(bean: CollectBean) {
        if (bean.program != currentProgram.program || bean.type != currentProgram.type) {
            currentProgram.program = bean.program
            currentProgram.type = bean.type
            notifyDataSetChanged()
        }
    }
}